package com.im.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.service.common.ImConstant;
import com.im.service.common.Result;
import com.im.service.domain.dto.ImChatDTO;
import com.im.service.domain.entity.ImChat;
import com.im.service.domain.entity.ImOrg;
import com.im.service.domain.entity.ImUser;
import com.im.service.domain.vo.UserVO;
import com.im.service.mapper.ImOrgMapper;
import com.im.service.mapper.ImUserMapper;
import com.im.service.service.ImChatService;
import com.im.service.service.ImUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.im.service.service.impl.ImChatServiceImpl.userChat;

/**
 * <p>
 * im-用户 服务实现类
 * </p>
 *
 * @author 刘志强
 * @since 2024-07-03
 */
@Service
public class ImUserServiceImpl extends ServiceImpl<ImUserMapper, ImUser> implements ImUserService {

    @Autowired
    @Lazy
    private ImChatService imChatService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ImOrgMapper orgMapper;


    @Override
    public Result register(ImUser imUser) {
        imUser.setId(null);
        this.save(imUser);
        ImChatDTO chatDTO = new ImChatDTO();
        chatDTO.setUserId(imUser.getId());
        chatDTO.setType(1);
        chatDTO.setTargetUserId(ImConstant.SysUserId);
        imChatService.createChat(chatDTO);
        return Result.ok();
    }

    @Override
    public Result<Integer> getUnread(Long userId) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        List<String> imChatList = hashOperations.values(userChat + StrPool.COLON + userId);
        Integer unread = 0;
        for (String str : imChatList) {
            ImChat imChat = JSONUtil.toBean(str, ImChat.class);
            unread = unread + imChat.getUnreadQuantity();
        }
        return Result.okData(unread);
    }

    @Override
    public Result<List<ImUser>> userList() {
        List<ImUser> list = list(Wrappers.lambdaQuery(ImUser.class)
                .ne(ImUser::getId, ImConstant.SysUserId)
                .orderByDesc(ImUser::getId)
        );
        return Result.okData(list);
    }

    @Override
    public Result<UserVO> userInfo(Long id) {
        ImUser imUser = this.getById(id);
        UserVO userVO = BeanUtil.toBean(imUser, UserVO.class);
        if (ObjUtil.isNotNull(imUser.getOrgId()) && imUser.getOrgId() > 0) {
            ImOrg imOrg = orgMapper.selectById(imUser.getOrgId());
            if (ObjUtil.isNotNull(imOrg)) {
                userVO.setOrgName(imOrg.getOrgName());
            }
        }
        return Result.okData(userVO);
    }

    @Override
    public Result<Map<String, ImUser>> userMap() {
        List<ImUser> list = this.list();
        Map<String, ImUser> userMap = new HashMap<>(list.size());
        for (ImUser imUser : list) {
            userMap.put(String.valueOf(imUser.getId()), imUser);
        }
        return Result.okData(userMap);
    }
}
